/**
 * 质控病种列表初始化
 */
var SdsQcentity = {
    id: "entityTree",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    entityId: 0,
    entityDesc: null
};
/**
 * 病种点击事件
 */
SdsQcentity.onClickEntity = function (e, treeId, treeNode) {
    if (treeNode.pId == null || treeNode.pId == 0) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体病种。");
        return false;
    }
    SdsQcentity.entityId = treeNode.id;
    SdsQcentity.entityDesc = treeNode.name
    //var indexType = $("input[name='indexType'][checked]").val();
    var indexTypeObj = $("input[name='indexType']:checked");
    //console.log(indexType)
    SdsCusIndexHeader.indexType = indexTypeObj.val();
    //查询病种指标
    SdsCusIndexHeader.seItem = null;
    SdsCusIndexHeader.search();
};

/**
 * 病种指标初始化
 */
var SdsCusIndexHeader = {
    id: "SdsCusIndexHeaderTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    indexType:0
};

/**
 * 初始化表格的列
 */
SdsCusIndexHeader.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        //{title: '病种字典', field: 'entityId', align: 'center', valign: 'middle', sortable: true, visible:false},
        {title: '指标名称', field: 'title', width:'20%',  align: 'left', valign: 'middle', sortable: true},
        {title: '指标字段', field: 'field', width:'20%', align: 'left', valign: 'middle', sortable: true},
        {title: '指标类型', field: 'headerType', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index) {
                if (value == 2) {
                    return "<span class='label label-success'>末级指标</span>";
                } else {
                    return "<span class='label label-warning'>父指标</span>";
                }
            }},
        {title: '是否有效', field: 'btIsActive', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }},
        {title: '是否重点指标', field: 'isImportant', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index) {
                if (value == 1) {
                    return "<i class='hisui-icon icon-star'></i>";
                }
            }},
        /*{title: '统计状态', field: 'analysisStatus', align: 'center', valign: 'middle', sortable: true,
            formatter:function(value,row,index){
                var result = '<a href="javascript:SdsCusIndexHeader.changeStatus(\''+row.id+'\',1)" title="更改状态"><div id="switch2" class="switch-small has-switch" style="margin-top:-3px" data-on="primary" data-off="gray" data-on-label="" data-off-label="" data-animated="false"><div class="switch-off"><span class="switch-left switch-small switch-primary">禁用</span><label class="switch-small">&nbsp;</label><span class="switch-right switch-small switch-gray">禁用</span></div></div></a>';;
                if(1==value){
                    result='<a href="javascript:SdsCusIndexHeader.changeStatus(\''+row.id+'\',0)" title="更改状态"><div id="switch2" class="switch-small has-switch" style="margin-top:-3px" data-on="primary" data-off="gray" data-on-label="" data-off-label="" data-animated="false"><div class="switch-on"><span class="switch-left switch-small switch-primary">启用</span><label class="switch-small">&nbsp;</label><span class="switch-right switch-small switch-gray">禁用</span></div></div></a>';
                }
                return result;
            }
        },*/
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
                var _html = '<a href="javascript:SdsCusIndexHeader.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
                _html += '<a href="javascript:SdsCusIndexHeader.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
                if (row.headerType==2){
                    _html += '<a href="javascript:SdsCusIndexHeader.compare(\''+row.id+'\',\''+row.name+'\')" title="对照"><i class="hisui-icon icon-transfer"></i></a>';
                }
                return _html;
            }}
        ];
    return columns;
};

/**
 * 检查是否选中
 */
SdsCusIndexHeader.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsCusIndexHeader.seItem = selected[0];
        return true;
    }
};

/**
 * 修改状态
 */
SdsCusIndexHeader.changeStatus = function(id,status){
    var ajax = new $ax(Sys.backendPath + "/sds/sdsindexheader/changeStatus", function (data) {
        Sys.success("修改成功!");
        SdsCusIndexHeader.table.refresh();
    }, function (data) {
        Sys.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set({"id":id,"status":status});
    ajax.start();
};

/**
 * 点击新增病种指标
 */
SdsCusIndexHeader.add = function () {
    if (SdsQcentity.entityId) {
        var index = Sys.open('新增病种指标', Sys.backendPath + '/sds/sdscusindexheader/to_add/' + SdsQcentity.entityId +"/"+ SdsCusIndexHeader.indexType +"/", ['800px', '480px'],function(layero, index, iframeWin){
            $(iframeWin.document).find("#entityId").attr("value", SdsQcentity.entityId);
            $(iframeWin.document).find("#entityDesc").attr("value", SdsQcentity.entityDesc);
        });
        this.layerIndex = index;
    } else {
        Sys.info("请先选中某一病种！");
    }

};

/**
 * 打开查看病种指标
 */
SdsCusIndexHeader.edit = function (id) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('指标详情',Sys.backendPath + '/sds/sdscusindexheader/to_update/' + id + "/",['800px', '480px'],function(layero, index, iframeWin){
            $(iframeWin.document).find("#entityId").attr("value", SdsQcentity.entityId);
            $(iframeWin.document).find("#entityDesc").attr("value", SdsQcentity.entityDesc);
        });
        this.layerIndex = index;
    }
};

SdsCusIndexHeader.exp = function () {
    if (SdsQcentity.entityId) {
        var operation = function(){
            Sys.download(Sys.backendPath + "/sds/sdsindexheader/export",{entityId:SdsQcentity.entityId});
        };
        Sys.confirm("是否导出到excel",operation);
    } else {
        Sys.info("请先选中某一病种！");
    }
};

SdsCusIndexHeader.template = function () {
    window.location.href = Sys.backendPath+"/static/backend/sds/sdsindexheader/tmp/XXXX_Template.xlsx";
};

SdsCusIndexHeader.imp = function () {
    var index = layer.open({
        type: 2,
        title: '导入指标配置',
        area: ['550px', '300px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Sys.backendPath + '/sds/sdsindexheader/to_import'
    });
    this.layerIndex = index;
};

/**
 * 删除病种指标
 */
SdsCusIndexHeader.del = function (id,name) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
    if (flag) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/sdscusindexheader/delete", function (data) {
                Sys.success("删除成功!");
                SdsCusIndexHeader.table.refresh();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("cusIndexHeaderId",id);
            ajax.start();
        };
        Sys.confirm("是否删除指标?",operation);
    }
};

/**
 * 病种指标对照病种项目
 */
SdsCusIndexHeader.compare = function (id,name) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('指标对照数据源',Sys.backendPath + '/sds/sdscusindexheader/to_compare/' + id + "/",['800px', '600px'],function(layero, index, iframeWin){
            $(iframeWin.document).find("#entityId").attr("value", SdsQcentity.entityId);
            $(iframeWin.document).find("#entityDesc").attr("value", SdsQcentity.entityDesc);
        });
        this.layerIndex = index;
    }
};


$HUI.radio("[name='indexType']",{
    onChecked:function(e,value){
        SdsCusIndexHeader.indexType = $(e.target).val();
        SdsCusIndexHeader.seItem = null;
        SdsCusIndexHeader.search();
    }
});


/**
 * 查询表单项目表列表
 */
SdsCusIndexHeader.search = function () {
    var queryData = {};
    queryData['qcEntity'] = SdsQcentity.entityId;
    queryData['indexType'] = SdsCusIndexHeader.indexType;
    SdsCusIndexHeader.table.refresh({query: queryData});
};

$(function () {
    //查询病种树
    var ztree = new $ZTree("entityTree", Sys.backendPath + "/sds/sdsqcentity/treebycode/Custom");
    ztree.bindOnClick(SdsQcentity.onClickEntity);
    ztree.init();
    SdsQcentity.ztree = ztree;
    fuzzySearch('entityTree','#seachKey',null,true);

    /*var indexHeaderColumns = SdsCusIndexHeader.initColumn();
    var indexHeaderTable = new BSTable(SdsCusIndexHeader.id, Sys.backendPath + "/sds/sdsindexheader/list", indexHeaderColumns);
    indexHeaderTable.setPaginationType("server");
    indexHeaderTable.setPageSize(10);
    indexHeaderTable.setSubHeight(-10);
    indexHeaderTable.setShowExport(false);
    indexHeaderTable.setShowRefresh(false);
    indexHeaderTable.setShowToggle(false);
    indexHeaderTable.setShowColumns(false);
    indexHeaderTable.setQueryParams({"qcEntity":0})
    indexHeaderTable.init();
    SdsCusIndexHeader.table = indexHeaderTable;*/

    var defaultColunms = SdsCusIndexHeader.initColumn();
    var table = new BSTreeTable(SdsCusIndexHeader.id, Sys.backendPath + "/sds/sdscusindexheader/list", defaultColunms);
    table.setExpandColumn(1);
    table.setIdField("id");
    table.setParentIdField("parentId");
    table.setExpandFirst(true);
    table.set("qcEntity",0);
    table.init();
    SdsCusIndexHeader.table = table;
});
